'use strict';

const Controller = require('egg').Controller;

class AdminController extends Controller {
  /**
   * 
   * @api {GET} /api/admin/getProjectBlog?projectId=4&limit=10&offset=0 管理台-获取项目所有博客(需要token 管理员身份)
   * @apiName 获取项目所有博客
   * @apiGroup 管理员模块
   * @apiVersion  1.0.0
   * 
   * 
   * @apiParam  {Number} projectId 项目id
   * 
   * 
   * 
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "成功操作",
          "data": {
              "count": 1,                 //博客总数
              "rows": [
                  {
                      "id": 62,                           //博客id
                      "userId": 28,                       //作者id
                      "userName": "mojitoo",              //作者昵称
                      "userAvatar": "www.baidu.com",      //作者头像
                      "projectId": 4,                     //所属项目id
                      "title": "html",                    //博客标题
                      "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //博客封面图
                      "content": "html",                  //博客内容
                      "read_count": 2,                    //博客浏览次数
                      "like_count": 1,                    //博客点赞次数
                      "comment_count": 2,                 //博客评论次数
                      "createdAt": "2021-10-12T10:33:47.000Z",        //博客创建时间
                      "updatedAt": "2021-10-13T02:37:09.000Z",        //博客最后更新时间
                      "deletedAt": null
                  }
              ]
          }
      }
   * 
   * 
   */
  //获取项目所有博客
  async getProjectBlog() {
    const { app, ctx, service } = this;
    let res = await service.blog.admin.getProjectBlog();
    if (res == '项目id错误') {
      return ctx.helper.errorModule(res);
    }
    if (res == '这不是您的项目') {
      return ctx.helper.errorModule(res);
    }
    return ctx.helper.successModule(res);
  }

  /**
   * 
   * @api {GET} /api/admin/getByKeyword?keyword=css&projectId=4 管理台-管理员根据关键词搜索博客
   * @apiName 管理员根据关键词搜索博客
   * @apiGroup 管理员模块
   * @apiVersion  1.0.0
   * 
   * 
   * @apiParam  {String} keyword 关键词
   * @apiParam  {Number} projectId 项目id
   * 
   * 
   * @apiSuccessExample {type} Success-Response:
      {
      "code": 200,
      "msg": "成功操作",
      "data": [
          {
              "id": 76,               //博客id
              "userId": 20,           //博客作者id
              "userName": "qwer",     //博客作者昵称
              "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //博客作者头像
              "title": "10-09",       //博客标题
              "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //博客封面图
              "content": "10-09",     //博客内容
              "read_count": 5,        //博客浏览次数
              "like_count": 1,        //博客点赞次数
              "comment_count": 0,     //博客评论次数
              "projectId":4,           //项目id
              "createdAt": "2021-10-09T02:54:20.000Z",    //博客创建时间
              "updatedAt": "2021-10-09T06:28:05.000Z",    //博客更新时间
              "deletedAt": null
          }
      ]
  }
   * 
   * 
   */
  //管理员根据关键词查找博客
  async getByKeyword() {
    const { app, ctx, service } = this;
    let checkRes = new this.ctx.helper.checkData('keyword').checkDataRes;
    //如果有缺少参数
    try {
      if (checkRes) {
        let res = await service.blog.admin.getByKeyword();
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res == '这不是您的项目') {
          return ctx.helper.errorModule(res);
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      return ctx.helper.errorModule(error);
    }
  }

  /**
   * 
   * @api {DELETE} /api/admin/deleteBlog 管理台-删除博客(需要token 管理员身份)
   * @apiName 删除博客
   * @apiGroup 管理员模块
   * @apiVersion  1.0.0
   * 
   * 
   * @apiParam  {Number} blogId 博客id
   * @apiParam  {Number} projectId 项目id
   * 
   * @apiSuccess (200) {type} name description
   * 
   * @apiParamExample  {type} Request-Example:
      {
          "blogId":62,
          "projectId":4
      }
   * 
   * 
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "删除成功",
          "data": []
      }
   * 
   * 
   */
  //删除博客
  async deleteBlog() {
    const { app, ctx, service } = this;
    //如果有缺少参数
    try {
      let res = await service.blog.admin.deleteBlog();
      if (res == '项目id错误') {
        return ctx.helper.errorModule(res);
      }
      if (res == '这不是您的项目') {
        return ctx.helper.errorModule(res);
      }
      return ctx.helper.successModule('删除成功');
    } catch (error) {
      return ctx.helper.errorModule(error);
    }
  }

  /**
   * 
   * @api {GET} /api/admin/getBlogComment?blogId=76&projectId=4&limit=10&offset=0 管理台-获取博客所有评论(需要token)
   * @apiName 获取博客所有评论
   * @apiGroup 管理员模块
   * @apiVersion  1.0.0
   * 
   * 
   * @apiParam  {Number} blogId 博客id
   * @apiParam  {Number} projectId 项目id
   * @apiParam  {Number} limit 返回评论量
   * @apiParam  {Number} offset 评论偏移量
   * 
   * 
   * 
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "成功操作",
          "data": {
              "count": 1,                             //评论总数
              "rows": [
                  {
                      "id": 29,                       //评论id
                      "userId": 29,                   //评论的用户的id
                      "userName": "mojito",           //评论的用户的昵称
                      "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //评论的用户的头像
                      "beRepliedUserId": null,        //被回复的用户的id
                      "beRepliedUserName": null,      //被回复的用户的昵称
                      "beRepliedUserAvatar": null,    //被回复的用户的头像
                      "blogId": 76,                   //博客id
                      "projectId": 4,                 //项目id
                      "authorId": 28,                 //博客作者id
                      "content": "last",              //评论内容
                      "parentCommentId": 0,           //父评论id
                      "likeCount": 0,                 //评论点赞次数
                      "createdAt": "2021-10-14T01:57:21.000Z",        //评论创建时间
                      "updatedAt": "2021-10-14T01:57:21.000Z",
                      "deletedAt": null
                  }
              ]
          }
      }
   * 
   * 
   */
  //获取博客所有评论
  async getBlogComment() {
    const { app, ctx, service } = this;
    let checkRes = ctx.helper.checkData('blogId', 'projectId');
    //如果有缺少参数
    if (checkRes) {
      return ctx.helper.lackModule(checkRes);
    }
    let res = await service.blog.admin.getBlogComment();
    if (res == '项目id错误') {
      return ctx.helper.errorModule(res);
    }
    if (res == '这不是您的项目') {
      return ctx.helper.errorModule(res);
    }
    return ctx.helper.successModule(res);
  }

  /**
   * 
   * @api {DELETE} /api/admin/deleteComment 管理台-删除评论(需要token 管理员身份)
   * @apiName 删除评论
   * @apiGroup 管理员模块
   * @apiVersion  1.0.0
   * 
   * 
   * @apiParam  {Number} commentId 评论id
   * @apiParam  {Number} blogId 博客id
   * @apiParam  {Number} projectId 项目id
   * 
   * 
   * @apiParamExample  {type} Request-Example:
      {
          "commentId":18,
          "blogId":50,
          "projectId":4
      }
   * 
   * 
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "删除成功",
          "data": []
      }
   * 
   * 
   */
  //删除评论
  async deleteComment() {
    const { app, ctx, service } = this;
    console.log(ctx.request.body);
    // let checkRes = new ctx.helper.checkData(this, 'commentId', 'blogId');
    // .checkDataRes;
    try {
      // if (checkRes) {
      let res = await service.blog.admin.deleteComment();
      if (res == '项目id错误') {
        return ctx.helper.errorModule(res);
      }
      if (res == '这不是您的项目') {
        return ctx.helper.errorModule(res);
      }

      return ctx.helper.successModule('删除成功');
      // }
    } catch (error) {
      return ctx.helper.errorModule(error);
    }
  }

  /**
   * 
   * @api {GET} /api/admin/getProjectComment?projectId=4&limit=10&offset=0 管理台-获取项目所有评论(需要token)
   * @apiName 获取项目所有评论
   * @apiGroup 管理员模块
   * @apiVersion  1.0.0
   * 
   * 
   * @apiParam  {Number} projectId 项目id
   * @apiParam  {Number} limit 评论返回量
   * @apiParam  {Number} offset 评论偏移量
   * 
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "成功操作",
          "data": {   
              "count": 5,                     //评论总数
              "rows": [
                  {
                      "id": 17,                   //评论id
                      "userId": 29,               //评论的用户id
                      "userName": "mojito",       //评论的用户昵称
                      "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",   //评论的用户头像
                      "beRepliedUserId": null,        //被回复的用户的id
                      "beRepliedUserName": null,      //被回复的用户的昵称
                      "beRepliedUserAvatar": null,    //被回复的用户的头像
                      "blogId": 62,                   //博客id
                      "projectId": 4,                 //项目id
                      "authorId": 28,                 //博客作者id
                      "content": "test",              //评论内容
                      "parentCommentId": 0,           //父评论id
                      "likeCount": 0,                 //评论点赞次数
                      "createdAt": "2021-10-13T01:58:05.000Z",        //评论创建时间
                      "updatedAt": "2021-10-13T01:58:05.000Z",
                      "deletedAt": null
                  },
                  {
                      "id": 22,
                      "userId": 29,
                      "userName": "mojito",
                      "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                      "beRepliedUserId": null,
                      "beRepliedUserName": null,
                      "beRepliedUserAvatar": null,
                      "blogId": 64,
                      "projectId": 4,
                      "authorId": 28,
                      "content": "qwer",
                      "parentCommentId": 0,
                      "likeCount": 0,
                      "createdAt": "2021-10-13T09:00:38.000Z",
                      "updatedAt": "2021-10-13T09:09:08.000Z",
                      "deletedAt": null
                  },
                  {
                      "id": 29,
                      "userId": 29,
                      "userName": "mojito",
                      "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                      "beRepliedUserId": null,
                      "beRepliedUserName": null,
                      "beRepliedUserAvatar": null,
                      "blogId": 76,
                      "projectId": 4,
                      "authorId": 28,
                      "content": "last",
                      "parentCommentId": 0,
                      "likeCount": 0,
                      "createdAt": "2021-10-14T01:57:21.000Z",
                      "updatedAt": "2021-10-14T01:57:21.000Z",
                      "deletedAt": null
                  },
                  {
                      "id": 32,
                      "userId": 29,
                      "userName": "mojito",
                      "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                      "beRepliedUserId": null,
                      "beRepliedUserName": null,
                      "beRepliedUserAvatar": null,
                      "blogId": 87,
                      "projectId": 4,
                      "authorId": 29,
                      "content": "comment1",
                      "parentCommentId": 0,
                      "likeCount": 0,
                      "createdAt": "2021-10-14T07:28:12.000Z",
                      "updatedAt": "2021-10-14T07:28:12.000Z",
                      "deletedAt": null
                  },
                  {
                      "id": 35,
                      "userId": 29,
                      "userName": "mojito",
                      "userAvatar": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",
                      "beRepliedUserId": null,
                      "beRepliedUserName": null,
                      "beRepliedUserAvatar": null,
                      "blogId": 87,
                      "projectId": 4,
                      "authorId": 29,
                      "content": "now11",
                      "parentCommentId": 0,
                      "likeCount": 0,
                      "createdAt": "2021-10-14T08:00:57.000Z",
                      "updatedAt": "2021-10-14T08:00:57.000Z",
                      "deletedAt": null
                  }
              ]
          }
      }
   * 
   * 
   */
  async getProjectComment() {
    const { app, ctx, service } = this;

    try {
      let res = await service.blog.admin.getProjectComment();
      return ctx.helper.successModule(res);
    } catch (error) {
      return ctx.helper.errorModule(error);
    }
  }
  /**
   * 
   * @api {GET} /api/admin/getBlogById?id=76&projectId=4 管理台-根据id查找博客
   * @apiName 根据id查找博客
   * @apiGroup 管理员模块
   * @apiVersion  1.0.0
   * 
   * 
   * @apiParam  {Number} id 博客id
   * @apiParam  {Number} projectId 项目id
   * 
   * 
   * @apiSuccessExample {type} Success-Response:
      {
          "code": 200,
          "msg": "成功操作",
          "data": {
              "id": 76,           //博客id
              "userId": 28,       //博客作者id
              "userName": "mojitoo",  //博客作者昵称
              "userAvatar": "www.baidu.com",  //博客作者头像
              "projectId": 4,         //项目id
              "title": "css",         //博客标题
              "cover": "https://blog-1251395798.cos.ap-guangzhou.myqcloud.com/image/_1632800038512_bluej.png",    //博客封面图
              "content": "css",       //博客内容
              "read_count": 5,        //博客浏览次数
              "like_count": 0,        //博客点赞次数
              "comment_count": 1,     //博客评论次数
              "createdAt": "2021-10-13T10:08:58.000Z",    //博客创建时间
              "updatedAt": "2021-10-26T07:13:41.000Z",    //博客最后更新时间
              "deletedAt": null,
              "BlogClassifies": [
                  {
                      "classifyId": 11,       //博客所属分类id
                      "classifyName": "HTML"  //博客所属分类名
                  },
                  {
                      "classifyId": 12,
                      "classifyName": "CSS"
                  }
              ],
              "LikeBlogs": []         //点赞该博客的用户id集合
          }
      }
   * 
   * 
   */
  async getBlogById() {
    const { app, ctx, service } = this;
    let checkRes = new this.ctx.helper.checkData(this, 'id').checkDataRes;

    try {
      if (checkRes) {
        let res = await service.blog.admin.getBlogById();
        if (res == '您不是管理员') {
          return ctx.helper.errorModule(res);
        }
        if (res == '项目id错误') {
          return ctx.helper.errorModule(res);
        }
        if (res == '这不是您的项目') {
          return ctx.helper.errorModule(res);
        }
        if (res === null) {
          return ctx.helper.errorModule('博客id不存在');
        }
        return ctx.helper.successModule(res);
      }
    } catch (error) {
      console.log(error);
      return ctx.helper.errorModule('操作失败');
    }
  }
}
module.exports = AdminController;
